<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Gets a parameter's type</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="reflectionparameter.getposition.html">« ReflectionParameter::getPosition</a></li>
      <li style="float: right;"><a href="reflectionparameter.hastype.html">ReflectionParameter::hasType »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.reflectionparameter.html">ReflectionParameter</a></li>
    <li>Gets a parameter's type</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="reflectionparameter.gettype" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ReflectionParameter::getType</h1>
  <p class="verinfo">(PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ReflectionParameter::getType</span> &mdash; <span class="dc-title">Gets a parameter&#039;s type</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionparameter.gettype-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ReflectionParameter::getType</strong></span>(): <span class="type"><span class="type">?</span><span class="type"><a href="class.reflectiontype.html" class="type ReflectionType">ReflectionType</a></span></span></div>

  <p class="para rdfs-comment">
   Gets the associated type of a parameter.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionparameter.gettype-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionparameter.gettype-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a <span class="classname"><a href="class.reflectiontype.html" class="classname">ReflectionType</a></span> object if a parameter type is
   specified, <strong><code>null</code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionparameter.gettype-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5236">
    <p><strong>示例 #1 <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> Usage as of PHP 7.1.0</strong></p>
    <div class="example-contents"><p>
     As of PHP 7.1.0, <span class="methodname"><a href="reflectiontype.tostring.html" class="methodname">ReflectionType::__toString()</a></span> is deprecated, and
     <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> <em class="emphasis">may</em> return an
     instance of <span class="classname"><a href="class.reflectionnamedtype.html" class="classname">ReflectionNamedType</a></span>. To get the name of the parameter type,
     <span class="methodname"><strong>ReflectionNamedType()</strong></span> is available in this case.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">someFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$param</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$param2</span><span style="color: #007700">)&nbsp;{}<br /><br /></span><span style="color: #0000BB">$reflectionFunc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'someFunction'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$reflectionParams&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionFunc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getParameters</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType1&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">ReflectionNamedType</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$reflectionType1</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int
NULL
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5237">
    <p><strong>示例 #2 <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> Usage before PHP 7.1.0</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">someFunction</span><span style="color: #007700">(</span><span style="color: #0000BB">int&nbsp;$param</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$param2</span><span style="color: #007700">)&nbsp;{}<br /><br /></span><span style="color: #0000BB">$reflectionFunc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionFunction</span><span style="color: #007700">(</span><span style="color: #DD0000">'someFunction'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$reflectionParams&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionFunc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getParameters</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$reflectionType2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionParams</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #0000BB">$reflectionType1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$reflectionType2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 7.0 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int
NULL
</pre></div>
    </div>
   </div>
   <div class="example" id="example-5238">
   <p><strong>示例 #3 <span class="methodname"><strong>ReflectionParameter::getType()</strong></span> Usage in PHP 8.0.0 and later</strong></p>
    <div class="example-contents"><p>
     As of PHP 8.0.0, this method may return a <span class="classname"><a href="class.reflectionnamedtype.html" class="classname">ReflectionNamedType</a></span> instance or
     a <span class="classname"><a href="class.reflectionuniontype.html" class="classname">ReflectionUnionType</a></span> instance.  The latter is a collection of the former.
     To analyze a type, it is often convenient to normalize it to an array of <span class="classname"><a href="class.reflectionnamedtype.html" class="classname">ReflectionNamedType</a></span>
     objects.  The following function will return an array of <code class="literal">0</code> or more <span class="classname"><a href="class.reflectionnamedtype.html" class="classname">ReflectionNamedType</a></span>
     instances.
    </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getAllTypes</span><span style="color: #007700">(</span><span style="color: #0000BB">ReflectionParameter&nbsp;$reflectionParameter</span><span style="color: #007700">):&nbsp;array<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$reflectionType&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$reflectionParameter</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getType</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">)&nbsp;return&nbsp;[];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$reflectionType&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">ReflectionUnionType<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getTypes</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;[</span><span style="color: #0000BB">$reflectionType</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-reflectionparameter.gettype-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="reflectionparameter.hastype.html" class="methodname" rel="rdfs-seeAlso">ReflectionParameter::hasType()</a> - Checks if parameter has a type</span></li>
    <li class="member"><span class="methodname"><a href="reflectiontype.tostring.html" class="methodname" rel="rdfs-seeAlso">ReflectionType::__toString()</a> - To string</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>